class StatsController < ApplicationController
  before_filter :check_authentication, :except => [:index]
  layout 'layout' 
  
  def index
    @machines = Machine.all

    @countos = Machine.find(
   :all, 
   :select => 'count(*) count, os', 
   :group => 'os' 
    )
    @test=[]
    @countos.each do |os|
      @test.push([os.os,os.count.to_i])
    end
   # @inc = Incident.find(
   #:all, 
   #:joins => :machine, 
   #:conditions => { :machines => { :id => @incident.machine_id } }
   # )
   @incram = getincbyyear(1,2012)
   @incdisk = getincbyyear(2,2012)
   @inccpu = getincbyyear(4,2012)
   @incagent = getincbyyear(5,2012)
   @incram2011 = getincbyyear(1,2011)
   @incdisk2011 = getincbyyear(2,2011)
   @inccpu2011 = getincbyyear(4,2011)
   @incagent2011 = getincbyyear(5,2011)
   sallesAll = Salle.find(
   :all, 
   :select => 'nom_salle, id', 
   :order => 'id' 
    )
    @salles=[]
    @inconnu=[]
    @linux=[]
    @mac=[]
    @win7=[]
    @XP=[]
    sallesAll.each do |sl|
      @salles.push(sl.nom_salle.to_s)
      @inconnu.push(Machine.count(:conditions => "salle_id = #{sl.id.to_s} AND os = 'Inconnu'"))
      @linux.push(Machine.count(:conditions => "salle_id = #{sl.id.to_s} AND os = 'Linux'"))
      @mac.push(Machine.count(:conditions => "salle_id = #{sl.id.to_s} AND os = 'Mac OS'"))
      @win7.push(Machine.count(:conditions => "salle_id = #{sl.id.to_s} AND os = 'Windows 7'"))
      @XP.push(Machine.count(:conditions => "salle_id = #{sl.id.to_s} AND os = 'Windows XP'"))
     
    end

   #@data = Machine.all
   #@series = @data.map{|w| w.incidents}
     
    respond_to do |format|
      format.html # index.html.erb

    end
    
  end
  
  def getincbyyear(type,date)
    @inctmps = Incident.all(
    :select => 'COUNT(*) AS count, date_detection AS day',
    :group => 'DATE(date_detection)',
    :conditions => "type_incident_id = '"+type.to_s+"' AND YEAR(date_detection) = '"+date.to_s+"'"
    )
    jinc = []
    @inctmps.each do |inctmp|
     
      month = inctmp.day.to_date.strftime("%m").to_i - 1
      day = inctmp.day.to_date.strftime("%d")
     
      jinc.push(["Date.UTC(1970,"+month.to_s+","+day.to_s+")",inctmp.count.to_i])
      #puts jinc
    # tmp = Time.local(year, month, day, hour, minute, second, usec ).to_i
      # @jincram.push([incramtmp.day.to_i+2764800000,incramtmp.count.to_i])
    end
   
    return jinc
  end

    #def updateGraph
     #monTab = maFunction
     #render Faire
      #mise à jour de ma div avec monTab
     #end 
    #end

end
